﻿Option Strict On

Imports System.Runtime.Serialization

Namespace FTP.Exceptions

    ''' <summary>
    ''' This exception is thrown when a general FTP exception occurs.
    ''' </summary>
    <Serializable()> _
    Public Class FtpException
        Inherits Exception
        Private _response As New FtpResponse()

        ''' <summary>
        ''' Constructor.
        ''' </summary>
        Public Sub New()
        End Sub

        ''' <summary>
        ''' Constructor.
        ''' </summary>
        ''' <param name="message">Exception message text.</param>
        Public Sub New(ByVal message As String)
            MyBase.New(message)
        End Sub


        ''' <summary>
        ''' Constructor.
        ''' </summary>
        ''' <param name="message">Exception message text.</param>
        ''' <param name="innerException">The inner exception object.</param>
        Public Sub New(ByVal message As String, ByVal innerException As Exception)
            MyBase.New(message, innerException)
        End Sub

        ''' <summary>
        ''' Constructor.
        ''' </summary>
        ''' <param name="message">Exception message text.</param>
        ''' <param name="response">Response object.</param>
        ''' <param name="innerException">The inner exception object.</param>
        Public Sub New(ByVal message As String, ByVal response As FtpResponse, ByVal innerException As Exception)
            MyBase.New(message, innerException)
            _response = response
        End Sub


        ''' <summary>
        ''' Constructor.
        ''' </summary>
        ''' <param name="message">Exception message text.</param>
        ''' <param name="response">Ftp response object.</param>
        Public Sub New(ByVal message As String, ByVal response As FtpResponse)
            MyBase.New(message)
            _response = response
        End Sub


        ''' <summary>
        ''' Constructor.
        ''' </summary>
        ''' <param name="info">Serialization information.</param>
        ''' <param name="context">Stream context information.</param>
        Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
            MyBase.New(info, context)
        End Sub

        ''' <summary>
        ''' Gets the last FTP response if one is available.
        ''' </summary>
        Public ReadOnly Property LastResponse() As FtpResponse
            Get
                Return _response
            End Get
        End Property


        ''' <summary>
        ''' Gets a message that describes the current exception.
        ''' </summary>
        Public Overrides ReadOnly Property Message() As String
            Get
                If _response.Code = FtpResponseCode.None Then
                    Return MyBase.Message
                Else
                    Return String.Format("{0}  (Last Server Response: {1}  {2})", MyBase.Message, _response.Text, _response.Code)
                End If


            End Get
        End Property
    End Class

End Namespace